<?php
// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------


namespace app\adminapi\controller\travel;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\travel\LineLists;
use app\adminapi\logic\travel\LineLogic;
use app\adminapi\validate\travel\LineValidate;
use app\common\model\user\User;


/**
 * Line控制器
 * Class LineController
 * @package app\adminapi\controller\travel
 */
class LineController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function lists()
    {
        return $this->dataLists(new LineLists());
    }


    /**
     * @notes 添加
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function add()
    {
        $params = (new LineValidate())->post()->goCheck('add');
        $params['user_id'] = User::where('account', $this->adminInfo['account'])->value('id');
        $result = LineLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(LineLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function edit()
    {
        $params = (new LineValidate())->post()->goCheck('edit');
        $result = LineLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(LineLogic::getError());
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function delete()
    {
        $params = (new LineValidate())->post()->goCheck('delete');
        LineLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function detail()
    {
        $params = (new LineValidate())->goCheck('detail');
        $result = LineLogic::detail($params);
        return $this->data($result);
    }

    /**
     * @notes  更改状态
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/23 18:23
     */
    public function updateStatus()
    {
        $params = (new LineValidate())->post()->goCheck('status');
        $result = LineLogic::updateStatus($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(LineLogic::getError());
    }

    /**
     * @notes 导入线路数据
     * @return \think\response\Json
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function import()
    {

        // 获取上传的文件
        $file = request()->file('file');
        if (empty($file)) {
            return $this->fail('请选择要上传的文件');
        }

        // 获取旅行社ID和用户ID（支持GET和POST方式）
        $travelAgencyId = request()->post('travel_agency_id') ?: request()->get('travel_agency_id');
        $userId = request()->post('user_id') ?: request()->get('user_id');

        if (empty($travelAgencyId)) {
            return $this->fail('缺少旅行社ID');
        }

        if (empty($userId)) {
            return $this->fail('缺少用户ID');
        }



        // 调用 Logic 层处理导入
        $result = LineLogic::import($file, $travelAgencyId, $userId);
        if ($result === true) {
            return $this->success('导入成功', [], 1, 1);
        }

        return $this->fail(LineLogic::getError() ?: '导入失败');
    }

    /**
     * @notes 下载导入模板
     * @return \think\response\File|\think\response\Json|\think\response\Response
     * @author admin
     * @date 2025/04/25 10:24
     */
    public function exportTemplate()
    {
        $filePath = runtime_path() . '/template/line_import_template.xlsx';

        if (!file_exists($filePath)) {
            return $this->fail('模板文件不存在');
        }

        return download($filePath)->name('线路导入模板.xlsx');
    }




}